package cn.uniquesoft.controller.base;

import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import cn.uniquesoft.service.ISearchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.uniquesoft.page.DataGrid;
import cn.uniquesoft.service.base.IBaseService;
import cn.uniquesoft.vo.AnswerVO;
import cn.uniquesoft.vo.base.BaseVO;

import javax.servlet.http.HttpServletResponse;


/**
 * @创建人 zyx
 * @创建时间 2017年6月20日 下午5:20:53
 * @描述 控制器基类
 */

@Controller
public class BaseController<T> {
	
	protected IBaseService<T>	baseService;
	protected String			indexName	= "";

	@RequestMapping(value = "/index")
	public String index() {
		return indexName;
	}

	@RequestMapping(value = "/save")
	@ResponseBody
	public AnswerVO save(T item) {
		AnswerVO answerVO = new AnswerVO();
		try {
			this.baseService.save(item);
			int itemId = ((BaseVO) item).getIid();
			if (itemId > 0) {
				answerVO.setSuccess(true);
				answerVO.setMsg("新增成功!");
			} else {
				answerVO.setSuccess(false);
				answerVO.setMsg("新增失败!");
			}
			answerVO.setData(itemId);
		} catch (Exception e) {
			answerVO.setSuccess(false);
			answerVO.setMsg("新增失败," + e.getMessage());
		}
		
		return answerVO;
	}

	@RequestMapping(value = "/edit")
	@ResponseBody
	public AnswerVO edit(T item) {
		AnswerVO answerVO = new AnswerVO();
		try {
			int result = this.baseService.edit(item);
			if (result > 0) {
				answerVO.setSuccess(true);
				answerVO.setMsg("修改成功!");
			} else {
				answerVO.setSuccess(false);
				answerVO.setMsg("修改失败!");
			}
			answerVO.setData(result);
		} catch (Exception e) {
			answerVO.setSuccess(false);
			answerVO.setMsg("修改失败," + e.getMessage());
		}

		return answerVO;
	}

	@RequestMapping(value = "/removeByIds")
	@ResponseBody
	public AnswerVO removeByIds(String ids) {
		AnswerVO answerVO = new AnswerVO();
		try {
			int result = this.baseService.removeByIds(ids);

			if (result > 0) {
				answerVO.setSuccess(true);
				answerVO.setMsg("删除成功!");
			} else {
				answerVO.setSuccess(false);
				answerVO.setMsg("删除失败!");
			}
			answerVO.setData(result);
		} catch (Exception e) {
			answerVO.setSuccess(false);
			answerVO.setMsg("删除失败," + e.getMessage());
		}

		return answerVO;
	}

	@RequestMapping(value = "/removeById")
	@ResponseBody
	public AnswerVO removeById(int  iid) {
		AnswerVO answerVO = new AnswerVO();
		try {
			int result = this.baseService.removeById(iid);

			if (result > 0) {
				answerVO.setSuccess(true);
				answerVO.setMsg("删除成功!");
			} else {
				answerVO.setSuccess(false);
				answerVO.setMsg("删除失败!");
			}
			answerVO.setData(result);
		} catch (Exception e) {
			answerVO.setSuccess(false);
			answerVO.setMsg("删除失败," + e.getMessage());
		}

		return answerVO;
	}


	@RequestMapping(value = "/findOneById")
	@ResponseBody
	public T findOneById(int id) {
		return this.baseService.findOneById(id);
	}

	@RequestMapping(value = "/findItems")
	@ResponseBody
	public List<T> findItems(T item) {
		return this.baseService.findAll(item);
	}

	@RequestMapping(value = "/pageQuery")
	@ResponseBody
	public DataGrid pageQuery(T item) {
		return this.baseService.pageQuery(item);
	}

	/**
	 * 绝对路径
	 * @param filePath
	 * @param response
	 */
	@RequestMapping(value = "/getPic")
	@ResponseBody
	public void getPic(String filePath, HttpServletResponse response){
		File file = new File(filePath);
		response.setContentType("image/*");
		OutputStream os = null;
		InputStream is = null;
		try {
			is = new FileInputStream(file);
			os = response.getOutputStream();
			byte[] bytes = new byte[1024];
			int count = 0;
			while ((count = is.read(bytes)) != -1){
                os.write(bytes,0,count);
            }
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				os.flush();
			} catch (IOException e) {
				e.printStackTrace();
			}
			try {
				os.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
			try {
				is.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}
